/**
 * yunmoxue@gmail.com
 * @return {[type]} [description]
 */
(function(){
	if(!window.niu) window.niu = {};
	var canvas = niu.canvas = function(options){
		//预加载的图画
		this._loadImg = '';
		if('loadImg' in options){
			console.log(options.loadImg);
			this._loadImg = options.loadImg;
		}
		//是否开始作画
		this.begining = false;
		//保存按钮
		this.saveBtn = $('#'+options.saveBtn);
		//保存按钮
		this.shareBtn = $('#'+options.shareBtn);
		//获取图画id
		this._canvas = $('#'+options.id);
		//得到图画的context
		this.context = this._canvas[0].getContext('2d');
		//设置图画样式
		this.setting();
		//绑定作画的动作
		this.bindEvent();
	}
	canvas.prototype.setting = function(){
		var _options = {
			'strokeStyle' : 'rgba(255,0,0,0.1)'//$('.colorpicker_current_color').css('backgroundColor').replace(')',',0.1)')
		};
		this._canvas.attr('width',this._canvas.width());
		this._canvas.attr('height',this._canvas.height());
		this.context.strokeStyle = _options.strokeStyle;
		// this.context.fillStyle = _options.strokeStyle;
		this.context.lineCap = "round";
		this.context.lineJoin="round";
		this.context.lineWidth = 5;
	}
	canvas.prototype.bindEvent = function() {
		var self = this;
		this._canvas.mousedown(function(e){self.onMouseDown(e);});
		this._canvas.mousemove(function(e){self.onMouseMove(e);});
		this._canvas.mouseup(function(e){self.onMouseUp(e);});
		this.saveBtn.click(function(e){self.save(e);})
		this.shareBtn.click(function(e){self.share(e);})
		if(this._loadImg!=''){
			$('.layer,.layer_content').show();
			var image = new Image();
			image.crossOrigin = "*";
			image.src = 'http://qian.u.qiniudn.com/'+this._loadImg;
			image.onload = function(){ 
				self.context.drawImage(image,0,0); 
				$('.layer,.layer_content').hide();
			};
		}
	};
	canvas.prototype.onMouseDown = function(e) {
		this.shareBtn.removeAttr('disabled');
		var strokeStyle= $('.colorpicker_current_color').css('backgroundColor').replace(')',',0.1)').replace('rgb','rgba');
		this.context.strokeStyle = strokeStyle;
		this.begining = true;
		var mouseX = e.pageX - this._canvas[0].offsetLeft;
		var mouseY = e.pageY - this._canvas[0].offsetTop;
		this.context.beginPath();
		this.context.moveTo(mouseX-1, mouseY-1);
		this.context.lineTo(mouseX, mouseY);
		this.context.closePath();
		this.context.stroke();
	};
	canvas.prototype.onMouseMove = function(e) {
		if(!this.begining) return false;
		var mouseX = e.pageX - this._canvas[0].offsetLeft;
		var mouseY = e.pageY - this._canvas[0].offsetTop;
		this.context.lineTo(mouseX, mouseY);
		this.context.stroke();
	};
	canvas.prototype.onMouseUp = function(e) {
		this.begining = false;
		this.context.save()
	};
	canvas.prototype.save = function(e){
		var image = this._canvas[0].toDataURL("image/png");
		image.replace("image/png", "image/octet-stream");
		var w=window.open(image,"smallwin","width=400,height=350");
		// window.location.href=image; 
	}
	canvas.prototype.share = function(e){
		this.shareBtn.attr('disabled','disabled');
		var image = this._canvas[0].toDataURL("image/png");
		image.replace("image/png", "image/octet-stream");
		$.post(
			'upload.php',
			{img:image},
			function(res){
				$('#console').html("分享url：<br/>http://qian.u.qiniudn.com/"+res.key+"<br/><br/>"+
					"分享给您的朋友继续作画:" + "<br/>" +
					"http://7niuniu.sinaapp.com/#"+res.key
					);
			},
			'json'
		);
		// window.location.href=image; 
	}
})();